/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sahd.logic.mail;

import beans.EmailBean;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



public class SenderEmailManager {

        private Properties emailProperties;
        private Session emailSession;
        private MimeMessage emailMessage;
        private Transport emailSender;

        private String sessionPassword;
        private String senderEmail;
        private String targetEmail;
        private String subjectEmail;
        private String textBodyEmail;
        
        private SenderEmailProtocolProperties emailProtocol;

        public SenderEmailManager(EmailBean emailBean){
            this.senderEmail = emailBean.getSenderEmail();
            this.targetEmail = emailBean.getTargetEmail();
            this.sessionPassword = emailBean.getSessionPassword();
            this.subjectEmail = emailBean.getEmailSubject();
            this.textBodyEmail = emailBean.getEmailBody();
        }

        private String getHostNameEmail(){
            final int accountType =1;
            String [] hostInformation = senderEmail.split("@");
            return "smtp."+ hostInformation[accountType];
        }

        
        private void configureProportiesSession(){
            emailProperties = new Properties();
            emailProperties.setProperty(emailProtocol.EMAIL_HOST_NAME_PROPERTIE, this.getHostNameEmail());
            emailProperties.setProperty(emailProtocol.EMAIL_TLS_PROTOCOL_PROPERTIE, emailProtocol.IS_TLS_ENEABLE);
            emailProperties.setProperty(emailProtocol.PROTOCOL_PORT_PROPERTIE, emailProtocol.PROTOCOL_PORT);
            emailProperties.setProperty(emailProtocol.USER_NAME_PROPERTIE, this.senderEmail);
            emailProperties.setProperty(emailProtocol.EMAIL_PASSWORD_PROPERTIE, emailProtocol.IS_PASSWORD_REQUIRED);
            emailSession = Session.getDefaultInstance(emailProperties);
        }
        
        private void configureMessageEmail() throws MessagingException{
            emailMessage = new MimeMessage(emailSession);
            emailMessage.setFrom(new InternetAddress(this.senderEmail));
            emailMessage.addRecipient(
                Message.RecipientType.TO,
                new InternetAddress(this.targetEmail));
            emailMessage.setSubject(this.subjectEmail);
            emailMessage.setText(this.textBodyEmail);
            }
        


        private void configureEmailSender() throws NoSuchProviderException, MessagingException{//sessionLogin
            emailSender = emailSession.getTransport(emailProtocol.PROTOCOL_NAME);
            emailSender.connect(this.senderEmail, this.sessionPassword);
            emailSender.sendMessage(emailMessage, emailMessage.getAllRecipients());
            emailSender.close();
        }

        //Se supone que este metodo es el que envia el email.
        public boolean sendEmail(){
            try {
                configureProportiesSession();
                configureMessageEmail();
                configureEmailSender();
                return true;
            } catch (MessagingException messagingException) {
                messagingException.printStackTrace();
                return false;
            }
        }
}